﻿#include "register.h"
#include "ui_register.h"
#include"odbclib.h"
#include<QSqlQuery>
#include<QSqlDatabase>
#include<QMessageBox>
#include<QDebug>

Register::Register(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
    setWindowTitle(QString::fromLocal8Bit("注册"));
     this->setWindowIcon(QIcon(":/photo/11.jfif"));
    //设置皮肤
    setfacer(4);
}

void Register::setfacer(int index)
{
    //换背景
    QImage _image;   //创建image
   //利用字符串拼接 完成photo
    QString str=QString(":/photo/back%1.jpg").arg(index);
   _image.load(str);
   setAutoFillBackground(true);   // 这个属性一定要设置
    QPalette pal(palette());
     pal.setBrush(QPalette::Window, QBrush(_image.scaled(size(), Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
    setPalette(pal);
}

Register::~Register()
{
    delete ui;
}

void Register::on_pushButton_2_clicked()
{
    QString id=ui->idtext->text();
    QString passwd1=ui->passwdtext->text();
    QString repeat1=ui->repeattext->text();
    QString name=ui->nametext->text();

    if(id==""||passwd1==""||repeat1==""||name==""){
        QMessageBox::information(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("信息未填写完整!"));

        return;
    }


    bool isture=ui->tiaokuan->isChecked();
    bool isstring=(passwd1==repeat1);
    int sum=0;
    if(ui->radiomale->isChecked()==true){
        sum=1;
    }
    if(ui->radiofemale->isChecked()==true){
        sum=2;
    }

    if(isture==true&&isstring==true){

        //数据库连接
        ODBClib *o=new ODBClib;
        o->connectdb();
        QSqlDatabase db=QSqlDatabase::database();

        //提前查询是否有重复的id
        QString str=QString("select * from myqicq_user where id='%1'").arg(id);
        QSqlQuery qsQuery=QSqlQuery(db);
        qsQuery.exec(str);
        qsQuery.last();
        int index1=qsQuery.at()+1;
        if(index1==1){

            QMessageBox::information(NULL,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("已存在该账号?请重新注册!"));

        }
        else{

            QString str2=QString("INSERT into myqicq_user VALUES('%1','%2','%3',%4,'%5')").arg(id).arg(passwd1).arg(name).arg(sum).arg("");
            QSqlQuery s=QSqlQuery(db);
            s.exec(str2);
            QMessageBox::information(NULL,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit(" 创建用户成功，赶紧登录吧! "));
            close();
        }

    }
    else if(isstring == false)
    {

        QMessageBox::information(NULL,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit(" 两次输入密码不一致 "));

    }
    else if(isture == false)
    {

        QMessageBox::information(NULL,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit(" 没有勾选用户须条款 "));

    }

}


void Register::on_pushButton_2_pressed()
{

}


void Register::on_pushButton_clicked()
{
    close();
    delete ui;
}

